<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <script type="text/javascript" src="../internal.js"></script>
    <style type="text/css">
        *{margin:0;padding:0;color: #838383;}
        table{font-size: 12px;margin: 10px;line-height: 30px}
        .txt{width:300px;height:21px;line-height:21px;border:1px solid #d7d7d7;}
    </style>
</head>
<body>
    <table>
        <tr>
            <td><label for="text"> <var id="lang_input_tel"></var></label></td>
            <td><input class="txt" id="tel" type="text"/></td>
        </tr>
        <tr>
            <td colspan="2" id="msg"></td>
        </tr>
    </table>
<script type="text/javascript">
    var range = editor.selection.getRange(),
        link = range.collapsed ? editor.queryCommandValue( "telphone" ) : editor.selection.getStart(),
        tel,
        text = $G('tel'),
        rangeLink = domUtils.findParentByTagName(range.getCommonAncestor(),'p',true),
        orgText;
    link = domUtils.findParentByTagName( link, "p", true );
    if(link){
      if(link.getAttribute( 'class' ) == 'wflink') {
        dialog.close();
      }
      tel = utils.html(link.getAttribute( 'href' ) || link.getAttribute( 'href', 2 ));

        if(rangeLink === link && !link.getElementsByTagName('img').length){
            text.removeAttribute('disabled');
            orgText = text.value = link[browser.ie ? 'innerText':'textContent'];
        }else{
            //text.setAttribute('disabled','true');
            //text.value = '只支持选中一个链接时生效！';
        }
    }else{
        if(range.collapsed){
            text.removeAttribute('disabled');
            text.value = '';
        }else{
            text.setAttribute('disabled','true');
            text.value = '只支持选中一个链接时生效！';
        }
    }
    $G("tel").value = text.value ? text.value: '';
    $focus($G("tel"));

    function handleDialogOk(){
        var href = $G('tel').value;
        if(!hrefStartWith(href)) {
            return;
        }
        if(href){
            var obj = {
                'href' : 'tel:'+href,
                'title' : href,
                'tel':'1'
            };
            //修改链接内容的情况太特殊了，所以先做到这里了
            //todo:情况多的时候，做到command里
            if(orgText && text.value != orgText){
                link[browser.ie ? 'innerText' : 'textContent'] =  obj.textValue = text.value;
                range.selectNode(link).select()
            }
            if(range.collapsed){
                obj.textValue = text.value;
            }
            editor.execCommand('link',utils.clearEmptyAttrs(obj) );
            dialog.close();
        }
    }
    dialog.onok = handleDialogOk;
    $G('tel').onkeydown = function(evt){
        evt = evt || window.event;
        if (evt.keyCode == 13) {
            handleDialogOk();
            return false;
        }
    };
    $G('tel').onkeyup = function(){
        if(!hrefStartWith(this.value)){
            $G("msg").innerHTML = "<span style='color: red'>"+lang.telPrompt+"</span>";
        }else{
            $G("msg").innerHTML = "";
        }
    };
    function hrefStartWith(href){
        if(/^(\d)*$/.test(href)){
            return true
        }else{
            return false
        }
    }
</script>
</body>
</html>
